import gdb

# A StackManager which encapsulates RTEMS-specific access to stack structures.
# USED BY: StackInjector.

class RTEMSStackManager(object):
    def __init__(self, threadIndex=1):
        self.stackString = ("((struct Thread_Control_struct *)"
            "_POSIX_Threads_Information.local_table[%d])->Start.Initial_stack" % threadIndex)
        self.stackAddress = self.stackString + ".area"
        self.stackSize = self.stackString + ".size"

    def getStackBeginning(self):
        stackBeginning = gdb.parse_and_eval(self.stackAddress + " + " + self.stackSize)
        return long(stackBeginning)
